package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.vo.ConfigBusinessVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ConfigBusinessAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3987812938323039952L;

	private Logger logger =Logger.getLogger(ConfigBusinessAction.class);
	
	private IConfigBusinessDao configBusinessDao;			//网关信息数据接口
	private List<ConfigBusinessVo> configBusinessVoList;	//网关信息列表
	private ConfigBusinessVo configBusinessVo;				//网关信息VO
	private String actionPath;								//操作成功跳转路径
	
	public String getConfigBusinessList(){
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String addConfigBusiness(){
		configBusinessVo = null;
		return SUCCESS;
	}
	
	public String editConfigBusiness(){
		if(configBusinessVo.getId() != 0){
			configBusinessVo = configBusinessDao.getConfigBusinessById(configBusinessVo.getId());
		}
		return SUCCESS;
	}
	
	public String updateConfigBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		if(configBusinessVo.getId() == 0){
			int count = configBusinessDao.insertConfigBusiness(configBusinessVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]网关信息成功", platUser.getLogin_username(),configBusinessVo.getSp_code()));
			}else{
				logger.info(String.format("[%s]添加[%s]网关信息失败", platUser.getLogin_username(),configBusinessVo.getSp_code()));
			}
		}else {
			int count = configBusinessDao.updateConfigBusiness(configBusinessVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]网关信息成功", platUser.getLogin_username(),configBusinessVo.getSp_code()));
			}else{
				logger.info(String.format("[%s]更新[%s]网关信息失败", platUser.getLogin_username(),configBusinessVo.getSp_code()));
			}
		}
		
		actionPath = "configBusiness/getConfigBusinessList.action";
		return SUCCESS;
	}
	
	public String deleteConfigBusiness(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = configBusinessDao.deleteConfigBusiness(configBusinessVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]网关信息成功", platUser.getLogin_username(),configBusinessVo.getSp_code()));
		}else{
			logger.info(String.format("[%s]删除[%s]网关信息失败", platUser.getLogin_username(),configBusinessVo.getSp_code()));
		}
		actionPath = "configBusiness/getConfigBusinessList.action";
		return SUCCESS;
	}

	public IConfigBusinessDao getConfigBusinessDao() {
		return configBusinessDao;
	}
	public void setConfigBusinessDao(IConfigBusinessDao configBusinessDao) {
		this.configBusinessDao = configBusinessDao;
	}
	public List<ConfigBusinessVo> getConfigBusinessVoList() {
		return configBusinessVoList;
	}
	public void setConfigBusinessVoList(List<ConfigBusinessVo> configBusinessVoList) {
		this.configBusinessVoList = configBusinessVoList;
	}
	public ConfigBusinessVo getConfigBusinessVo() {
		return configBusinessVo;
	}
	public void setConfigBusinessVo(ConfigBusinessVo configBusinessVo) {
		this.configBusinessVo = configBusinessVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}